<?php
class Order_AssignController extends Zend_Controller_Action
{
	/**
	* 订单列表   
	*
	*/
	private $_s;
	public function preDispatch(){
		$this->_s=new Order_Service_Order();
		$this->tplDirectory = "order/assign/";	//控制对应模板目录
	}	
	
	public function indexAction(){
		$value = '1';
		$field = 'st_status';
		$shipType = Shipping_Service_ShipType::getAllByValue($value,$field);
		$shipTypeArr = array();
		foreach($shipType as $v){
			$shipTypeArr[$v['st_code']] = $v;
		}
		//ksort($shipTypeArr);
		
		$processStatus = Order_Service_Order::getAllProcessStatus();

		$pickusers= User_Service_User::getByfield('ud_id', 12);
        
		$this->view->shipType = $shipTypeArr;
		$this->view->processStatus = $processStatus;
		$this->view->pickusers = $pickusers;
		
		
		echo Cff::renderTpl($this->tplDirectory.'index.tpl',"layout");
	}
	
	public function detailAction(){
		$orders_code=$this->_request->getParam('orders_code','');
		$this->view->orders_code=$orders_code;
		echo Cff::renderTpl($this->tplDirectory.'detail.tpl',"layoutheader");
	}
	/**
	*返回订单产品详细
	*Param op_id
	*/
	public function prodetailAction(){
		$op_id=$this->_request->getParam('id','');
		if(!empty($op_id)){
			$opRows=Order_Service_Product::getById($op_id);
		}
		$this->view->op_id=$op_id;
		$this->view->orders_code=$opRows['orders_code'];
		$this->view->product_id=$opRows['product_id'];
		$this->view->op_declared_value=$opRows['op_declared_value'];
		$this->view->product_sku=$opRows['product_sku'];
		$this->view->op_quantity=$opRows['op_quantity'];
		$this->view->op_title=$opRows['op_title'];
		$this->view->op_description=$opRows['op_description'];
		echo Cff::renderTpl($this->tplDirectory.'prodetail.tpl',"layoutheader");
	}

	/**
	*产品列表页面
	*Param orders_code
	*/
	public function productAction(){
		$orders_code=$this->_request->getParam('orders_code','');
		$rows=$this->_s->getByOrdersCode($orders_code);
		$this->view->customer_id=$rows['customer_id'];
		$this->view->orders_code=$orders_code;
		echo Cff::renderTpl($this->tplDirectory.'productlist.tpl',"layoutheader");
	}

	/**
	*根据用户ID返回产品列表
	*Param customer_id
	*retrun json
	*/
	public function productlistAction(){
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize');
		$customer_id=$this->_request->getParam('customer_id','');
		$product=$this->_request->getParam('product_id','');//ID或名称
		$intpage = ($intpage<1)?1:$intpage; 
		$condition=array(
			'product'=>$product,
			'customer_id'=>$customer_id,
			'product_status'=>1,//激活状态
		);
		$count=Product_Service_Product::getByCondition($condition,'count(*)','',0,0);
		$data=array(
			'count'=>0,
			'ask'=>0
			);
		if($count>0){
			$Rows=Product_Service_Product::getByCondition($condition,'*',array('product_id desc'),$pageSize,$intpage);
			foreach($Rows as $key => $value){
				$products=Warehouse_Service_Products::getByProductId($value['product_id']);
				$Rows[$key]['ws_code']=$products['ws_code'];
				$Rows[$key]['wp_quantity']=$products['wp_quantity'];
			}
			$data=array(
				'count'=>$count,
				'result'=>$Rows,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));
	}
	
	public function listAction(){
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize');
		
		$orders_status=$this->_request->getParam('orders_status_sch','');	//搜索表单提交的状态
		$customerId=$this->_request->getParam('customerId','');	//搜索表单提交的客户代码
		$customerId = trim($customerId);
		$searchType = $this->_request->getParam('searchType','orders_code');//代码类型
		$code = $this->_request->getParam('code','');	//搜索表单提交的订单代码
		$code = trim($code);
		$orders_code_list = array();
		if($code){				
			if($searchType=="pickup_code"){				
				
				$model_orderPickupMap = new Order_Model_OrderPickupMap();
				$orderPickupMaps = $model_orderPickupMap->getByOpCode($code);
				if(is_array($orderPickupMaps)){
					foreach($orderPickupMaps as $v){
						$orders_code_list[] = $v['orders_code'];				
					}		
				}
			}elseif($searchType=="ship_code"){		
				$model_orderShip = new Order_Model_OrderShip();
				$orderShip = $model_orderShip->getByTrackingNumber($code);
				if(is_array($orderShip)){					
					foreach($orderShip as $v){
						$orders_code_list[] = $v['orders_code'];
					}	
				}	
				
			}elseif($searchType=="ref_code"){		
				$orders =  Order_Service_Order::getByfield("customer_order_code",$code);
				if(is_array($orders)){					
					foreach($orders as $v){
						$orders_code_list[] = $v['orders_code'];
					}	
				}	
				
			}else{
				$orders_code_list[] = $code;
			}
			
			if(empty($orders_code_list)){
				$orders_code_list[] = md5(time());//随机数
			}
			
		}
		
		
//		print_r($orders_code_list);die();
		$payment_status=$this->_request->getParam('payment_status','');
		$warehouse_id=$this->_request->getParam('warehouse_id','');
		$shipping_method = $this->_request->getParam('shipping_method','');
		
		$browsetype=$this->_request->getParam('browsetype','fold');//收展产品

		$intpage = ($intpage<1)?1:$intpage; 
		$condition=array(
			'orders_code'=>$orders_code_list,
			'process_status'=>$orders_status,
			'payment_status'=>$payment_status,
			'warehouse_id'=>$warehouse_id,
			'shipping_method'=>$shipping_method,
			'customer_id'=>$customerId,
		);
		$count=$this->_s->getByCondition($condition,'count(*)','',0,0);
		$data=array(
			'count'=>0,
			'ask'=>0
			);
		if($count>0){
			$service_product = new Product_Service_Product();
			$Rows=$this->_s->getByCondition($condition,'*','order_print_id asc',$pageSize,$intpage);
			foreach($Rows as $key => $value){
				$Rows[$key]['orders_status']=$this->_s->getOrderStatus($value['orders_status']);
				$Rows[$key]['process_status_ch']=$this->_s->getProcessStatus($value['process_status']);
// 				$pickupmap  = Order_Service_OrderPickupMap::getByfield("orders_code",$value['orders_code']);
// 				$Rows[$key]['opm_create_date'] = empty($pickupmap)?"":$pickupmap['opm_create_date'];//打印时间
				
				if($browsetype!='fold'){
					$order_product=Order_Service_Product::getByCondition(array('orders_code'=>$value['orders_code']));
					foreach($order_product as $k => $v){
						$w_product=Warehouse_Service_Products::getByProductId($v['product_id']);
						$order_product[$k]['ws_code']=$w_product['ws_code'];
						$category = $service_product->getProductCategory($v['product_id']);
						$order_product[$k]['category'] = "";
						if($category){
							$order_product[$k]['category'] = $category['poc_value'];							
						}
					}
					$Rows[$key]['order_product']=$order_product;
				}
				
				if($value['pickup_user_id']>0){
					$pickupUserRow=User_Service_User::getByUserId($value['pickup_user_id']);
					$Rows[$key]['pickup_user_name']=$pickupUserRow['name'];
				}else{
					$Rows[$key]['pickup_user_name']='<span style="color: blue;">'."[未分配]".'</span>';
				}
				
				$country=Common_Service_Country::getByValue($value['consignee_country_id'],'country_id');
				$Rows[$key]['consignee_country_id']=$country['country_code'];
			}
			$data=array(
				'count'=>$count,
				'result'=>$Rows,
				'ask'=>1
			);
		}
//		print_r($data);exit;
		die(Zend_Json::encode($data));

	}

	
	
	/**
	*返回订单详细
	*getParam orders_code
	*return json
	*/
	public function infoAction(){
		$orders_code=$this->_request->getParam('orders_code');
		$return=array(
			'ask'=>0,
			'result'=>null
		);
		if(!empty($orders_code)){
			$rows=$this->_s->getByOrdersCode($orders_code);
			$rows['orders_status']=$this->_s->getOrderStatus($rows['orders_status']);
			$proRow=Order_Service_Product::getByCondition(array('orders_code'=>$orders_code));
			$subtotal=0;//订单总金额
			if(!empty($proRow)){
				foreach($proRow as $key => $value){
					$product=Product_Service_Product::getByProductId($value['product_id']);
					$proRow[$key]['product_title']=$product['product_title'];
					$Picture=Product_Service_Attached::getProductPicture($value['product_id']);//获取图片
					$proRow[$key]['product_img']=$Picture['pa_path'];
					$proRow[$key]['subtotal']=$value['op_quantity']*$value['op_unit_price'];//产品总价
					$subtotal+=$proRow[$key]['subtotal'];
				}
			}
			$return=array(
				'ask'=>1,
				'result'=>$rows,
				'productList'=>$proRow,
				'subtotal'=>$subtotal,
			);
		}
		die(Zend_Json::encode($return));
	}
	
	/**
	*
	*Param ws_id
	*/
	public function assignpickuserAction(){
		$order_code=$this->_request->getParam('order_code','');
		$pickuserid=$this->_request->getParam('pickusers','');
		
		if($order_code&&$pickuserid){
			
			$session = new Zend_Session_Namespace('userAuth');
			$user_id=$session->userID;
			
			$productSer = new Product_Service_Product();
			$adapter = $productSer->getAdapter();
			$adapter->beginTransaction();
			
			try {
			
			$updateRows=array(
					"pickup_user_id"=>$pickuserid,
					"last_modified_time"=>date("Y-m-d H:i:s")
			);
			$rowOrder=Order_Service_Order::getByOrdersCode($order_code);
			
			$updateOrderResult=Order_Service_Order::updateByOrdersCode($updateRows,$order_code);
			if(!$updateOrderResult){
				$return=array("ask"=>0,"msg"=>"err");
				$adapter->rollback();
				die(Zend_Json::encode($return));
			
			}
			
			$model = new Order_Model_History();
			$logRow = array (
					"orders_code" =>$order_code ,
					"osh_type" => "Revise",
					"osh_statu" => $rowOrder['process_status'],
					"osh_create_date" => date("Y-m-d H:i:s"),
					"osh_comments" => "order pickup_user_id from ".$rowOrder['pickup_user_id'].' to '.$pickuserid,
					"osh_remark" => '',
					"user_id" => $user_id,
					"osh_ip" => $_SERVER['REMOTE_ADDR'],
			
			);
			$model->create($logRow);
			
			$adapter->commit();
			$return = array(
					"ask" => 1,
					"msg" => 'ok'
			);
			die(Zend_Json::encode($return));
			
			} catch (Exception $e) {
			
				$adapter->rollback();
				$return = array(
						"ask" => 0,
						"msg" => $e->getMessage()
				);
				die(Zend_Json::encode($return));
			}
		}
	}
	
	public function batchassignpickuserAction(){
		//$order_code=$this->_request->getParam('select_orders','');
		$select_orders=$this->_request->getParam('select_orders','');
		
		$pickuserid=$this->_request->getParam('batchpickusers','');
		
		if($select_orders&&$pickuserid){
			
			$session = new Zend_Session_Namespace('userAuth');
			$user_id=$session->userID;
				
			$productSer = new Product_Service_Product();
			$adapter = $productSer->getAdapter();
			$adapter->beginTransaction();
				
			try {
			
			//获取未修改前的订单
				
			$getUnEditOrder=Order_Service_Order::getByOrdersIds($select_orders);
			
			$updateRows=array(
					"pickup_user_id"=>$pickuserid,
					"last_modified_time"=>date("Y-m-d H:i:s")
					);
			$updateOrderResult=Order_Service_Order::updateByOrdersIds($updateRows,$select_orders);
			if(!$updateOrderResult){
				$return=array("ask"=>0,"msg"=>"ok");
				$adapter->rollback();
				die(Zend_Json::encode($return));
			}
			
			
			$model = new Order_Model_History();
			
			foreach ($getUnEditOrder as $val){
	
				  $logRow = array (
							"orders_code" =>$val['orders_code'] ,
							"osh_type" => "Revise",
							"osh_statu" => $val['process_status'],
							"osh_create_date" => date("Y-m-d H:i:s"),
							"osh_comments" => "order pickup_user_id from ".$val['pickup_user_id'].' to '.$pickuserid,
							"osh_remark" => '',
							"user_id" => $user_id,
							"osh_ip" => $_SERVER['REMOTE_ADDR'],
					
					);
					$model->create($logRow);

				
			}
			
			$adapter->commit();
			$return = array(
					"ask" => 1,
					"msg" => 'ok'
			);
			die(Zend_Json::encode($return));
			
			} catch (Exception $e) {
					
				$adapter->rollback();
				$return = array(
						"ask" => 0,
						"msg" => $e->getMessage()
				);
				die(Zend_Json::encode($return));
			}
			

			
		}
		
		
	}
	
	
}